<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/14
 * Time: 16:50
 */

namespace Admin\Controller;


class GoodscommentController extends AdminController
{
    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $where['status']=1;
        $where['level']=1;
        $uid=session('user_auth.uid');
        //判断权限
        if($uid!=1){
            $group_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
            //用户组类别 10-商户组
            if($group_id['group_id']==10){
                $shop_info=M('member')->where(array('uid'=>$group_id['uid']))->find();
                $where['shop_id']=$shop_info['shop_id'];
            }
        }
        //查询数据
        $listsCount = M('goods_comment')->where($where)->count();
        $Page       = new \Think\Page($listsCount,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $lists = M('goods_comment')->where($where)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($lists as $ke=>$ve){
            $shop=M('shop')->where(array('id'=>$ve['shop_id']))->find();
            $lists[$ke]['shop_name']=$shop['shop_name'];
            $goods=M('goods')->where(array('id'=>$ve['goods_id']))->find();
            $lists[$ke]['goods_name']=$goods['goods_name'];
            $lists[$ke]['create_time']=date('Y-m-d H:i:s',time());
            //评论人类型 1-用户  2-商家
            if($ve['type']==1){
                $goods=M('usermember')->where(array('id'=>$ve['uid']))->find();
                $lists[$ke]['username']=$goods['username'];
            }else{
                $lists[$ke]['username']=$shop['shop_name'];
            }
        }
        $this->assign('list',$lists);
        $this->assign('group_id',$group_id['group_id']);
        $this->assign('_page',$show);
        $this->display();
    }

    public function edit(){
        header("Content-Type: text/html;charset=utf-8");
        if(IS_POST){
            $up_comment=M('goods_comment')->where(array('id'=>$_POST['id']))->find();
            $data['content']=$_POST['content'];
            $data['create_time']=time();
            $data['pid']=$_POST['id'];
            $data['goods_id']=$up_comment['goods_id'];
            $data['shop_id']=$up_comment['shop_id'];
            $data['uid']=$up_comment['shop_id'];
            $data['level']=$up_comment['level']+1;
            $data['type']=2;
            $res=M('goods_comment')->add($data);
            if($res){
                $this->success('回复成功');
            }else{
                $this->error('回复失败');
            }
        }else{
            $id=I('id');
            $uid=session('user_auth.uid');
            //判断权限
            if($uid!=1){
                $group_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
                //用户组类别 10-商户组
                if($group_id['group_id']==10){
                    $shop_info=M('member')->where(array('uid'=>$group_id['uid']))->find();
                }
            }
            $comment=comment_info($id);
            $son_comment_ids=$this->son_comment_id($id);
            if(!empty($son_comment_ids)){
                $comment['son_status']=1;
            }
            $son_comments=comment_infos($son_comment_ids);
            $this->assign('list',$comment);
            $this->assign('son_comment',$son_comments);
            $this->assign('group_id',$group_id['group_id']);
            $this->display();
        }
    }

    public function son_comment_id($id){
        global $son_comment_ids;
        $comment=M('goods_comment')->where(array('pid'=>$id))->order('create_time asc')->select();
        if($comment){
            foreach ($comment as $k=>$v){
                $son_comment_ids[]=$v['id'];
                $this->son_comment_id($v['id']);
            }
        }
        return $son_comment_ids;
    }
}